package com.phone.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phone.common.api.CommonPage;
import com.phone.common.api.CommonResult;
import com.phone.modules.pms.dto.PmsProductDetailDTO;
import com.phone.modules.pms.dto.PmsProductQueryDTO;
import com.phone.modules.pms.dto.PmsProductResultDTO;
import com.phone.modules.pms.model.PmsProduct;
import com.phone.modules.pms.service.PmsProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangHao
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    PmsProductService productService;

    @ApiOperation("前台展示，返回手机详情")
    @RequestMapping(value = "/getDetail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getDetail(@PathVariable("id") Long id) {
        PmsProductDetailDTO detail = productService.getDetail(id);
        return CommonResult.success(detail);
    }

    @ApiOperation("前台展示手机")
    @RequestMapping(value = "/searchList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult searchList(PmsProductQueryDTO productQuery) {
        List<PmsProductResultDTO> list = productService.searchList(productQuery);
        return CommonResult.success(list);
    }

    @ApiOperation("查询手机")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getList(PmsProductQueryDTO productQueryDTO,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page page = productService.list(productQueryDTO, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation(value = "添加手机")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody PmsProduct pmsProduct) {
        boolean result = productService.createProduct(pmsProduct);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据手机id获取手机编辑信息")
    @RequestMapping(value = "/updateInfo/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getUpdateInfo(@PathVariable Long id) {
        PmsProduct product = productService.getUpdateInfo(id);
        return CommonResult.success(product);
    }

    @ApiOperation(value = "修改手机信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProduct pmsProduct) {
        boolean result = productService.updateProduct(id, pmsProduct);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("批量修改删除状态")
    @RequestMapping(value = "/update/deleteStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateDeleteStatus(@RequestParam("ids") List<Long> ids,
                                           @RequestParam("deleteStatus") Integer deleteStatus) {
        boolean result = productService.updateDeleteStatus(ids, deleteStatus);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

}

